package com.telit.uav.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.telit.common.entity.DataReturn;
import com.telit.common.entity.PageData;
import com.telit.common.enums.ResultCode;
import com.telit.common.util.StringUtils;
import com.telit.uav.dao.CommUavDao;
import com.telit.uav.dto.UavFlyRecordDTO;
import com.telit.uav.entity.AppUavTrackDetail;
import com.telit.uav.service.CommUavService;
import com.telit.uav.service.FlightDetailsService;
import com.telit.uav.service.TeamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * @Author:zhangwei
 * @Description:
 * @Time:Created on 2021/9/18 8:06
 */
@Api(tags = "03---飞行记录相关接口", description = "/flight-detail")
@RestController
@RequestMapping("/flight-detail")
public class FlightDetailsController {

    @Autowired
    FlightDetailsService flightDetailsService;

    @Autowired
    private CommUavDao commUavDao;

    @Autowired
    private TeamService teamService;

    @Autowired
    private CommUavService commUavService;


    @ApiOperation("01---分页条件查询飞行记录列表")
    @GetMapping("/listFlyRecordByPage")
    public DataReturn listFlyRecordByPage(@RequestParam(defaultValue = "1") Integer current,
                                          @RequestParam(defaultValue = "10") Integer size,
                                          UavFlyRecordDTO uavFlyRecordDTO) {
        IPage<UavFlyRecordDTO> uavFlyRecordDTOIPage = flightDetailsService.listFlyRecordByPage(current, size,
                uavFlyRecordDTO);
        List<UavFlyRecordDTO> records = uavFlyRecordDTOIPage.getRecords();
        if (!CollectionUtils.isEmpty(records)) {
            records.forEach(dto -> {
                // 飞行距离/最大飞行高度保留两位
                dto.setFlyDistance(StringUtils.formatTwoDecimal(dto.getFlyDistance()));
                dto.setMaxHigh(StringUtils.formatTwoDecimal(dto.getMaxHigh()));
            });
        }
        return DataReturn.ok(new PageData<>((int) uavFlyRecordDTOIPage.getTotal(), current, size, records));
    }

    @GetMapping("/getTimeAxisByTaskIdAndUseId")
    @ApiOperation("根据任务ID查询时间轴信息")
    public DataReturn getTimeAxisByTaskIdAndUseId(@RequestParam(required = true) Long taskId,
                                                  @RequestParam(required = true) Long userId){
        return DataReturn.ok(flightDetailsService.getTimeAxisByTaskIdAndUseId(taskId,userId));
    }

    @GetMapping("/getUavInfoById")
    @ApiOperation("查询单个无人机信息")
    public DataReturn getUavInfoByUavCode(@RequestParam(required = true) String uavCode) {
        try {
            return DataReturn.ok(commUavService.getUavInfoByUavCode(uavCode));
        } catch (Exception e) {
            e.printStackTrace();
            return DataReturn.failure(ResultCode.OPERATE_ERROR, "查询失败！");
        }
    }

    @GetMapping("/listUavTrackDetail")
    @ApiOperation("根据任务id、userId获取飞行轨迹信息")
    public DataReturn<List<AppUavTrackDetail>> getFlytInfoAtTime(@RequestParam(required = true) Long taskId,
                                                                 @RequestParam(required = true) Long userId) {

        return DataReturn.ok(flightDetailsService.listUavTrackDetail(taskId, userId));
    }

    @GetMapping("/queryTrackDetail")
    @ApiOperation("根据飞行轨迹信息")
    public DataReturn queryTrackDetail(@RequestParam(required = true) Long taskId,
                                        @RequestParam(required = true) Long userId){

        return DataReturn.ok(flightDetailsService.queryTrackDetail(taskId,userId));
    }

    @GetMapping(value = "/uavUserList",produces = "application/json;charset=UTF-8")
    @ApiOperation("分手列表")
    public DataReturn uavUserList(){
        return DataReturn.ok(teamService.uavUserList());
    }

}
